// AirsoftTech.dk //////// WIRE PIN AN COMPONENT LAYOUT ///////// int MOSFET_PIN = 5; // The Digital pin that attaches to the MOSFET gate, to turn it on and off. int TRIGGER_PIN = A0; // The analog pin that attaches to the trigger pin float R1 = 100; // 100Kohm // The Voltage devider resistor R1 float R2 = 10; // 100Kohm // The Voltage devider resistor R2 float MIN_BATTERY_VOLTAGE = 3.6 * 2; // I Want 3.6V per cell on 2 Cell battery. 2 * 3.6 = 7.2; //////// CONFIG VALUES ///////// int Max_ON_Time = 200; // The time in MS for a full burst cycle... //////// INTERNAL VALUES ///////// int TrigerStatus = LOW; // The state of the trigger LOW => Not pressed, HIGH => Pressed int TriggerReadValue = 0; // The value read from the analog trigger pin. => 0-1024 int CurrentSleepTime = 0; // The ammount of time the mosfet has been on. float voltageDevider = 0.0909; //(R2 / R1 + R2) float Vmax = 55; // Input voltage that would give 5v in voltage devider. float VStep = 0.0537109375; // Each voltage step in the analog pin will indicate this voltage is given. float CurrentBatteryVoltage = 7.4; // Current battery Voltage. // The setup routine runs once when you press reset. void setup() { // Initialize the digital pin as an output. pinMode(13, OUTPUT); // LED pin pinMode(MOSFET_PIN, OUTPUT); // Set the Mosfet pin as an output so that we can send power to the mosfet. digitalWrite(MOSFET_PIN, LOW); // Make sure we start with power OFF! digitalWrite(13, LOW); // Make sure we start with power OFF! voltageDevider = R2 / (R1 + R2); // Make ure that Vmax = 5.0 / voltageDevider; VStep = Vmax / 1024.0; // Serial.begin(9600); } // The loop routine runs over and over again forever. void loop() { ReadTrigger(); // Update the trigger state if(TrigerStatus == HIGH) // If the trigger is pushed. { SetMosfet(HIGH); // First turn the Mosfet On while (TrigerStatus == HIGH) { if(CurrentSleepTime < Max_ON_Time){ // If we have not completed a cycle CurrentSleepTime = CurrentSleepTime + 1; // Leave the mosfet on, and increase time counter. } else { SetMosfet(LOW); // If a full cycle has gone, turn the Monfet Off. } delay(1); // Sleep for one milisecond ReadTrigger(); // Update the trigger status (We stay in the loop untill the trigger is released) } SetMosfet(LOW); // Make sure the Mosfet is off when the trigger is released CurrentSleepTime = 0; // Reset time counter when the trigger is released. } } // This function updates the trigger status when it's called. void ReadTrigger() { int TriggerReadValue = analogRead(TRIGGER_PIN); CurrentBatteryVoltage = VStep * TriggerReadValue; // Serial.print("\t output = "); // Serial.print(TriggerReadValue); // Serial.print(" - "); // Serial.println(CurrentBatteryVoltage); // delay(10); // Sleep for one milisecond TrigerStatus = LOW; if(TriggerReadValue > 20) { if(CurrentBatteryVoltage >= MIN_BATTERY_VOLTAGE ) { TrigerStatus = HIGH; } } } // This function set the Mosfet state void SetMosfet(int val) { // Serial.print("\t DigitalWrite = "); // Serial.println(val); // delay(10); // Sleep for one milisecond digitalWrite(13, val); // turn the LED on / off to indicate what the mosfet should be dooing digitalWrite(MOSFET_PIN, val); // turn the Monfet on / off }